package com.example.newdemo.view.image.svg;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.graphics.drawable.PictureDrawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;

public class SVGImageView extends androidx.appcompat.widget.AppCompatImageView {
    private static Method setLayerTypeMethod = null;

    public SVGImageView(Context context) {
        super(context);

        try {
            setLayerTypeMethod = View.class.getMethod("setLayerType", Integer.TYPE, Paint.class);
        } catch (NoSuchMethodException var3) {
        }

    }

    public SVGImageView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);

        try {
            setLayerTypeMethod = View.class.getMethod("setLayerType", Integer.TYPE, Paint.class);
        } catch (NoSuchMethodException var4) {
        }

        this.init(attrs, 0);
    }

    public SVGImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        try {
            setLayerTypeMethod = View.class.getMethod("setLayerType", Integer.TYPE, Paint.class);
        } catch (NoSuchMethodException var5) {
        }

        this.init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        TypedArray a = this.getContext().getTheme().obtainStyledAttributes(attrs, null, defStyle, 0);

        try {
            int resourceId = a.getResourceId(0, -1);
            if (resourceId != -1) {
                this.setImageResource(resourceId);
                return;
            }

            String url = a.getString(0);
            if (url != null) {
                Uri uri = Uri.parse(url);
                if (this.internalSetImageURI(uri, false)) {
                    return;
                }

                this.setImageAsset(url);
            }
        } finally {
            a.recycle();
        }

    }

    public void setSVG(SVG mysvg) {
        if (mysvg == null) {
            throw new IllegalArgumentException("Null value passed to setSVG()");
        } else {
            this.setSoftwareLayerType();
            this.setImageDrawable(new PictureDrawable(mysvg.renderToPicture()));
        }
    }

    public void setImageResource(int resourceId) {
        try {
            SVG svg = SVG.getFromResource(this.getContext(), resourceId);
            this.setSoftwareLayerType();
            this.setImageDrawable(new PictureDrawable(svg.renderToPicture()));
        } catch (SVGParseException var3) {
            Log.e("SVGImageView", String.format("Error loading resource 0x%x: %s", resourceId, var3.getMessage()));
        }

    }

    public void setImageURI(Uri uri) {
        this.internalSetImageURI(uri, true);
    }

    public void setImageAsset(String filename) {
        try {
            SVG svg = SVG.getFromAsset(this.getContext().getAssets(), filename);
            this.setSoftwareLayerType();
            this.setImageDrawable(new PictureDrawable(svg.renderToPicture()));
        } catch (SVGParseException var3) {
            Log.e("SVGImageView", "Error loading file " + filename + ": " + var3.getMessage());
        } catch (FileNotFoundException var4) {
            Log.e("SVGImageView", "File not found: " + filename);
        } catch (IOException var5) {
            Log.e("SVGImageView", "Unable to load asset file: " + filename, var5);
        }

    }

    private boolean internalSetImageURI(Uri uri, boolean isDirectRequestFromUser) {
        InputStream is = null;

        try {
            is = this.getContext().getContentResolver().openInputStream(uri);
            SVG svg = SVG.getFromInputStream(is);
            this.setSoftwareLayerType();
            this.setImageDrawable(new PictureDrawable(svg.renderToPicture()));
            return true;
        } catch (FileNotFoundException var15) {
            if (isDirectRequestFromUser) {
                Log.e("SVGImageView", "File not found: " + uri);
            }
        }  catch (com.example.newdemo.view.image.svg.SVGParseException e) {
            Log.e("SVGImageView", "Error loading file " + uri + ": " + e.getMessage());
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException var14) {
            }

        }

        return false;
    }

    private void setSoftwareLayerType() {
        if (setLayerTypeMethod != null) {
            try {
                int LAYER_TYPE_SOFTWARE = View.class.getField("LAYER_TYPE_SOFTWARE").getInt(new View(this.getContext()));
                setLayerTypeMethod.invoke(this, LAYER_TYPE_SOFTWARE, null);
            } catch (Exception var2) {
                Log.w("SVGImageView", "Unexpected failure calling setLayerType", var2);
            }

        }
    }
}
